#!/usr/bin/env python
import sys

import util

def output(sym_cnt):
    dist = util.prob_distribution(sym_cnt)
    e = util.entropy(sym_cnt)
    print>>delta_summary_file, "#seg_idx = ", seg_idx
    print>>delta_summary_file, "#num syms = ", len(sym_cnt)
    print>>delta_summary_file, "#num records = ", sum([v for v in sym_cnt.values()])
    print>>delta_summary_file, "#entropy = ", e
    print>>delta_summary_file, "#sym_cnt = ", repr(sym_cnt)
    print>>delta_summary_file

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print "cat raw | delta_enc.py [seg_len] [name]"
        print "seg_len: # of records in a segment. -1 means all records"

        sys.exit(0)

    name = sys.argv[2]
    seg_len = int(sys.argv[1])
    if seg_len <= 0:
        seg_len = sys.maxint
        delta_file = open("delta_seglen-all_%s.txt"%(name), "w")
        delta_summary_file = open("delta_seglen-all_%s.summary.txt"%(name), "w")
    else:
        delta_file = open("delta_seglen-%d_%s.txt"%(seg_len, name), "w")
        delta_summary_file = open("delta_seglen-%d_%s.summary.txt"%(seg_len, name), "w")

    assert(seg_len > 0)

    seg_idx = -1
    sym_cnt = {}
    for (i,l) in enumerate(sys.stdin):
        l = l.strip().lstrip("0")

        if ((i % seg_len) == 0 ):
            old = eval(l)
            sym = l
            if sym_cnt:
                print>>delta_file
                output(sym_cnt)
            seg_idx += 1
            sym_cnt  = {}
            print>>delta_file, "#seg_idx = ", seg_idx
            print>>delta_file,  sym
            sym_cnt[sym] = 1
            continue
        else:
            new = eval(l)
            delta = new - old
            old = new
            print>>delta_file, str(delta)
            try:
                sym_cnt[str(delta)] += 1
            except:
                sym_cnt[str(delta)] = 1
    output(sym_cnt)

    delta_file.close()
    delta_summary_file.close()
